package levelEditor;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

// Everything to read and write level files from the system
public class FileToolset {

    private static final Logger logger = Logger.getLogger(FileToolset.class.getName());
    private static final String LEVEL_INDEX_FILENAME= "levelIndex.inv";
    private static final String ASSET_FILENAME= "assets";
    private static final String LEVELS_FILENAME= "Levels";
    
    public FileToolset() {
        Logger.getLogger(FileToolset.class.getName()).setLevel(Level.INFO); // SEVERE  INFO
    }
    
    public boolean writeLevel(String levelName, String content) {
        String folderName = getLevelsFolderName();
        if (!writeFile(folderName + System.getProperty("file.separator") + levelName + ".lvl", content)){
            return false;
        }
        return writeLevelIndex();
    }
    
    private boolean writeLevelIndex() {
        List<String> list = new ArrayList<String>();
        String folderName = getLevelsFolderName();
        list = getFolderList();
        String content = "";
        for(int i = 0; i<list.size();i++ ){
           content += list.get(i); 
           content += "\n";
        }
        return writeFile(folderName + System.getProperty("file.separator") + LEVEL_INDEX_FILENAME, content);
    }
    
    private boolean writeFile(String filePathName, String content)
    {   
        logger.log(Level.INFO, "save file name: {0}", new Object[]{filePathName});
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(filePathName));
            out.write(content);
            out.close();
        }
        catch (IOException e)
        {
            logger.log(Level.WARNING, "Catched exception in FileToolset:writeFile: {0}", new Object[]{filePathName});
            return false;
        }
        return true;
    }    
    
    public List<String> getFolderList() {
        List<String> list = new ArrayList<String>();
        String folderName = getLevelsFolderName();
        File dir = new File(folderName);
        File[] filesList = dir.listFiles();
        if(!isFolderFound(folderName)){
            return list;
        }
        for(File f : filesList){
            if(f.isFile()){
                if(!f.getName().equalsIgnoreCase(LEVEL_INDEX_FILENAME)){
                    String name = f.getName();
                    list.add(name.replace(".lvl", ""));
                }
            }
        }
        return list;
    }
    
    private String getLevelsFolderName() {
        String root = System.getProperty("user.dir");
        String separator = System.getProperty("file.separator");
        String folderName = root + separator + ASSET_FILENAME + separator + LEVELS_FILENAME;
        return folderName;
    }                
        
    public boolean isFolderFound(String folderName) {
        File dir = new File(folderName);
        if(dir.isDirectory()){
            return true;
        }
        return false;
    }
}
